package LeetCode.exercise;

public class LC43 {
    public String multiply(String num1, String num2) {
        if(num1.charAt(0) == '0' || num2.charAt(0) == '0'){
            return "0";
        }
        int l1 = num1.length(), l2 = num2.length();
        int[] arr = new int[l1 + l2];
        for(int i = l1 - 1; i >= 0; i--){
            for(int j = l2 - 1; j >= 0; j--){
                arr[l1-1-i + l2-1-j] += (num1.charAt(i) -'0') * (num2.charAt(j) - '0');
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < arr.length; i++){
            sb.append(arr[i] % 10);
            if(i+1 < arr.length) {
                arr[i + 1] += arr[i] / 10;
            }
        }
        sb.reverse();
        while(sb.charAt(0) == '0'){
            sb.deleteCharAt(0);
        }
        return sb.reverse().toString();
    }
}
